<html>
<head><meta charset="utf-8"><title>Why does this require `T: Default`? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20does.20this.20require.20.60T.3A.20Default.60.3F.html">Why does this require `T: Default`?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="210852412"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20does%20this%20require%20%60T%3A%20Default%60%3F/near/210852412" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> memoryleak47 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20does.20this.20require.20.60T.3A.20Default.60.3F.html#210852412">(Sep 22 2020 at 10:34)</a>:</h4>
<p>Hello, I'm writing a own PhantomData type, and I'm wondering about the compiler error.<br>
It tells me (in the <code>fn new() -&gt; Phantom&lt;T&gt;</code>) that it requires <code>T: Default</code>, which is weird because <code>Phantom&lt;T&gt;</code> does not contain <code>T</code> but just <code>Void</code>.</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#[derive(Default)]</span><span class="w"></span>
<span class="k">struct</span> <span class="nc">Void</span><span class="p">;</span><span class="w"></span>

<span class="k">trait</span><span class="w"> </span><span class="n">GetVoid</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">type</span> <span class="nc">V</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="n">GetVoid</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">type</span> <span class="nc">V</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Void</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="cp">#[derive(Default)]</span><span class="w"></span>
<span class="k">struct</span> <span class="nc">Phantom</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">(</span><span class="o">&lt;</span><span class="n">T</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">GetVoid</span><span class="o">&gt;</span>::<span class="n">V</span><span class="p">);</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="n">Phantom</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">new</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">Phantom</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nb">Default</span>::<span class="n">default</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="210852472"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20does%20this%20require%20%60T%3A%20Default%60%3F/near/210852472" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20does.20this.20require.20.60T.3A.20Default.60.3F.html#210852472">(Sep 22 2020 at 10:35)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/issues/26925">#26925</a></p>



<a name="210852747"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20does%20this%20require%20%60T%3A%20Default%60%3F/near/210852747" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> memoryleak47 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20does.20this.20require.20.60T.3A.20Default.60.3F.html#210852747">(Sep 22 2020 at 10:39)</a>:</h4>
<p>Thanks! (:</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>